package su.Ner;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Hashtable;

import su.FeatureSelection.KeyValuePair;

public class U {
	static double truncation(double d){
		int _d=(int)(d*100);
		return((double)_d)/100;
	}
	
	static void PersonNameExtractor(String inFile,String outDir) throws IOException{
		BufferedReader br=new BufferedReader(new FileReader(inFile));
		BufferedWriter bwP=new BufferedWriter(new FileWriter(outDir+"person.txt"));
		BufferedWriter bwL=new BufferedWriter(new FileWriter(outDir+"location.txt")); 
		BufferedWriter bwO=new BufferedWriter(new FileWriter(outDir+"organization.txt"));
		BufferedWriter bwT=new BufferedWriter(new FileWriter(outDir+"title.txt"));
		
		while(true){
			String line=br.readLine();
			if(null==line ||line.equals("")) break;
			NerDataSequence ds=new NerDataSequence(line);
			for(int i=0;i<ds.length();i++){
				if(ds.y(i)==1 ||ds.y(i)==2 ||ds.y(i)==3){
					bwP.write(ds.x(i)+"\n");
				}
				else if(ds.y(i)==4 ||ds.y(i)==5 ||ds.y(i)==6)
					bwL.write(ds.x(i)+"\n");
				else if(ds.y(i)==7 ||ds.y(i)==8 ||ds.y(i)==9)
					bwO.write(ds.x(i)+"\n");
				else if(ds.y(i)==10 ||ds.y(i)==11 ||ds.y(i)==12)
					bwT.write(ds.x(i)+"\n");
			}
				
		}
		
		br.close();
		bwP.close();
		bwL.close();
		bwO.close();
		bwT.close();
		
	}
	
	public static void printHashTable(Hashtable ht)
	{
		System.out.println();
		for(Object o : ht.keySet()){
			System.out.println(o+"\t\t"+ht.get(o));
		}
		System.out.println();
	}


	public static void error(String statement){
		System.err.println(statement);
		System.exit(-1);
	}

	public static void TestFileCompare(String root,String file1,String file2) throws IOException{
		BufferedReader sr1=new BufferedReader(new FileReader(root+file1));
		BufferedReader sr2=new BufferedReader(new FileReader(root+file2));
		
		int i=1;
		while(true){
			String line1=sr1.readLine();
			String line2=sr2.readLine();
			if(line1==null||line1.equals("") ) break;
			
			i++;
			if(! new NerDataSequence(line1).hasSameLabels(new NerDataSequence(line2))){
				System.out.println(i);
				System.out.println(line1);
				System.out.println(line2);
				System.out.println();
			}
		
		}
		
		sr1.close();
		sr2.close();
	}

	public static void printLineInTxt(String s) throws IOException{
		BufferedWriter bw=new BufferedWriter(new FileWriter("C:\\Documents and Settings\\Su\\桌面\\a.txt"));
		bw.write(s+"\n");
		bw.close();
		
	}
	
	public static String removeId(String s){
		String[] sa=s.split(":");
		return sa[0]+":"+sa[2];
	}
	
	public static void main(String[] args) throws IOException {
		//TestFileCompare(CrfNer.root+"\\data\\","test_base.txt","test_P_T.txt");
		//System.out.println(removeId("CCase.lower:-19:9"));
		//getWDiff("C:\\Documents and Settings\\Su\\桌面\\diff\\base.txt","C:\\Documents and Settings\\Su\\桌面\\diff\\previous.txt");
		//PersonNameExtractor("C:\\Documents and Settings\\Su\\桌面\\CrfNer\\CRF\\ner\\data\\train.txt","C:\\Documents and Settings\\Su\\桌面\\CrfNer\\CRF\\ner\\data\\");
//		Hashtable<String ,Integer> ht =new Hashtable<String, Integer>();
//		ht.put("asdf", 123);
		//System.out.println(Double.MIN_VALUE>0);
	}
	
	public static void getWDiff(String base, String previous) throws IOException{
		BufferedReader brB =new BufferedReader(new FileReader(base));
		BufferedReader brP =new BufferedReader(new FileReader(previous));
		
		HashMap<String, Double> b=new HashMap<String, Double>();
		HashMap<String, Double> p=new HashMap<String, Double>();
		
		while(true){
			String line=brB.readLine();
			if(line==null||line.equals("")) break;
			String[] sa=line.split("\t");
			b.put(sa[0], Double.parseDouble(sa[1]));
		}
		
		while(true){
			String line=brP.readLine();
			if(line==null||line.equals("")) break;
			String[] sa=line.split("\t");
			p.put(sa[0], Double.parseDouble(sa[1]));
		}
		
		for(String s:b.keySet()){
			try{
			if(s.substring(0,2).equals("W_") && b.get(s)-p.get(s)>3)
				System.out.println(s +"\t"+(b.get(s)-p.get(s)));
			}catch(Exception e){
				
			}
		}
		
		brB.close();
		brP.close();
	}

	public static double[] getLambda() throws Exception{
        BufferedReader in=new BufferedReader(new FileReader(".\\ner\\data\\crf_61.92"));
        int numF = Integer.parseInt(in.readLine());
        double[] lambda = new double[numF];
        int pos = 0;
        String line;
        while((line=in.readLine())!=null) 
            lambda[pos++] = Double.parseDouble(line);
        return lambda;
	}

	public static void sort(ArrayList<KeyValuePair> list){
		// sort the features according to conditional probability
		Comparator comp = new Comparator() {
			public int compare(Object o1, Object o2) {
				KeyValuePair p1 = (KeyValuePair) o1;
				KeyValuePair p2 = (KeyValuePair) o2;
				if (p1.value < p2.value)
					return 1;
				else
					return 0;
			}
		};
		Collections.sort(list, comp);
	}
	
	@SuppressWarnings("unchecked")
	public static Hashtable copyHashtable(Hashtable ht){
		Hashtable res=new Hashtable();
		for(Object key:ht.keySet()){
			res.put(key, ht.get(key));
		}
		return res;
	}

	public static double getMin(double[] da){
		double res=da[0];
		for(double d:da)
			if(d<res)
				res=d;
		return res;
			
	}
	
	public static String getFeatureFName(String f){
		return f.substring(0,f.indexOf('.'));
	}

	public static String getFeaturePredicate(String f){
		return f.substring(0,f.indexOf(':'));
	}
	
	public static int getFeatureId(String f){
		int end=f.lastIndexOf(':');
		int begin =f.lastIndexOf(":", end-1);
		return Integer.parseInt(f.substring(begin+1,end));
	}
	
	public static void add1(Hashtable<String, Integer> m,String s){
		int i=0;
		if(null!=m.get(s))
			i=m.get(s);
		m.put(s, i+1);
	}
}


